package springboot.springMVC.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @RestController=@Controller+@ResponseBody
 */
//@RestController
@Controller
//@ResponseBody
@RequestMapping("/h1")
public class IndexController {

    @RequestMapping("/m1")
    public Object index(){
        return "/index1.html";
    }
@ResponseBody//去掉这个注解会认为是视图，找不到这个视图，报错
    @RequestMapping("/m2")
    public Object index2(){
        return "我是index2界面";
    }

    @RequestMapping("/m3")//这里是复习前面的页面跳转
    public Object index3(){
        return "/test.html";
    }

    @RequestMapping("/m4")
    @ResponseBody//没有这行代码会认为是视图，找不到这个会报错
    public Object index4(){
        return "<h1>test</h1>";
    }

    @RequestMapping("/m5")
    public Object index5(){
        return "/a.js";
    }

    @RequestMapping("/m6")
    public Object index6(){
        return "/b.css";
    }


    /**
     * @ResponseBody 是告诉 Spring，不需要通过视图解析器去渲染视图，
     * 而是将方法返回值直接写入 HTTP 响应体中，通常是 JSON 格式、XML 格式或者纯文本。
     * @return
     */
    @RequestMapping("/m7")
    @ResponseBody
    public HashMap<String,String> returnJson(){
        HashMap<String,String> map = new HashMap<>();
        map.put("a","1");
        map.put("b","2");
        map.put("c","3");
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/m8",produces = "application/json;charset=utf-8")
    public String returnJson2(){
        return "{\"sucess\":true\n" +
                "\"name\":等我}";
    }

    @RequestMapping("/m9")
    @ResponseBody
    public String setHeader(HttpServletResponse a){
        a.setHeader("MyHeader","MyHeaderValue");
        return "success";
    }
}
